package com.miss.spjyx

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.miss.spjyx.model.Danju
import com.miss.spjyx.model.Eqv

/**
 * 单据adapter
 *
 * @author wl
 * @since 2019/08/11 19:01
 */
class DanjuAdapter(var context: Context, var ruku: Boolean) : RecyclerView.Adapter<DanjuAdapter.ViewHolder>() {

    var data: List<Danju>? = null
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_danju, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, i: Int) {
        val res = data?.get(i)
        if (res != null) {
            holder.tv_serial.text = res.code
            makeItList(holder.ll_it_list, res.eqlist)
            holder.itemView.setOnClickListener {
                CRkuActivity.startThis(context, ruku, res.activityId, res.hallId)
            }
        }
    }

    override fun getItemCount(): Int {
        return data?.size ?: 0
    }

    fun makeItList(ll: LinearLayout, list: Array<Eqv>) {
        val inflater = LayoutInflater.from(context)
        ll.removeAllViews()
        for (eq in list) {
            val view = inflater.inflate(R.layout.item_danju_it, ll, false)
            val tv_it_name = view.findViewById<TextView>(R.id.tv_it_name)
            val tv_it_ocount = view.findViewById<TextView>(R.id.tv_it_ocount)
            val tv_it_rcount = view.findViewById<TextView>(R.id.tv_it_rcount)
            tv_it_name.text = eq.equipmentName
            tv_it_ocount.text = eq.rfmNumber.toString()
            tv_it_rcount.text = eq.actNumber.toString()
            ll.addView(view)
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tv_serial = itemView.findViewById<TextView>(R.id.tv_serial)
        val ll_it_list = itemView.findViewById<LinearLayout>(R.id.ll_it_list)
    }
}
